package com.wisdytech.linkdcs.tool.service.impl;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.system.model.SysUser;
import com.wisdytech.linkdcs.tool.dao.ToolReplacePlanDao;
import com.wisdytech.linkdcs.tool.model.ToolReplacePlan;
import com.wisdytech.linkdcs.tool.service.IToolReplacePlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhanghailin
 * @since 2018-06-08
 */
@Service
@Transactional
public class ToolReplacePlanServiceImpl extends BaseService<ToolReplacePlanDao, ToolReplacePlan> implements IToolReplacePlanService {

    @Resource
    private ToolReplacePlanDao toolReplacePlanDao;

    @Override
    public PageInfo<ToolReplacePlan> getToolReplacePlanByFilter(String toolCode, String toolName, Filter filter) {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.isNotBlank(toolCode)) {
            params.put("toolCode", toolCode);
        }
        if (StringUtils.isNotBlank(toolName)) {
            params.put("toolName", toolName);
        }
        List<ToolReplacePlan> list = this.beforeList(ToolReplacePlanDao.class, filter).getToolReplacePlanByFilter(params);
        PageInfo<ToolReplacePlan> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public Boolean saveOrUpdateToolReplacePlan(ToolReplacePlan toolReplacePlan, SysUser operator) {
        Integer operateCount = 0;
        if(operator == null)
            throw new ServiceException("操作人不能为空");
        if(toolReplacePlan == null)
            throw new ServiceException("参数错误");

        if (StringUtils.isNotBlank(toolReplacePlan.getPkId())) {
            toolReplacePlan.setUpdateDate(new Date());
            toolReplacePlan.setUpdateUser(operator.getUsername());
            operateCount = toolReplacePlanDao.updateById(toolReplacePlan);
        } else {
            toolReplacePlan.setPkId(BaseUtil.getUUID());
            toolReplacePlan.setCreateDate(new Date());
            toolReplacePlan.setDelFlag(CommonConstants.DEL_FLAG);
            toolReplacePlan.setCreateUser(operator.getUsername());
            operateCount = toolReplacePlanDao.insert(toolReplacePlan);
        }
        return operateCount > 0;
    }

    @Override
    public Boolean deleteToolReolacePlan(String pkIds, SysUser operator) {
        if(operator == null)
            throw new ServiceException("操作人不能为空 ");
        String[] deletePkIds = pkIds.split(",");
        if(deletePkIds.length == 0) return true;
        List<String> pkIdCollection = Arrays.asList(deletePkIds);
        Integer deleteCount = toolReplacePlanDao.deleteBatchIds(pkIdCollection);
        return deleteCount > 0;
    }

    @Override
    public ToolReplacePlan getToolReplacePlanById(String pkId) {
        if(StringUtils.isBlank(pkId)) {
            return null;
        }
        return this.toolReplacePlanDao.getToolReplacePlanById(pkId);
    }
}
